package com.htzz.collection.config;


import com.htzz.collection.core.mongo.MongoTemplate;
import com.mongodb.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
public class MongoConfig {


    @Bean
    @Primary
    public MongoClient mongoClient(){
        if(isCluster){
            //集群mongo
            int length = -1;
            ServerAddress adress;
            List<ServerAddress> adresses = new ArrayList<>();
            for (String host : hosts.split(",")) {
                if((length = host.indexOf(":")) != -1){
                    String ip = host.substring(0, length);
                    Integer port =Integer.valueOf(host.substring(length+1));
                    adress=new ServerAddress(ip, port);
                    adresses.add(adress);
                }
            }
            MongoCredential credential = MongoCredential.createCredential(userName, auth, passWord.toCharArray());
            MongoClient client = new MongoClient(adresses, Arrays.asList(credential),mongoClientOptions());
            return client;
        }else{
            ServerAddress adress=new ServerAddress(hosts, port);
            if(authentication){
                MongoCredential credential = MongoCredential.createCredential(userName, auth, passWord.toCharArray());
                MongoClient client = new MongoClient(adress,Arrays.asList(credential),mongoClientOptions());
                return client;
            }else{
                MongoClient client = new MongoClient(adress,mongoClientOptions());
                return client;
            }
        }
    }

    @Bean(name="priMongo")
    public MongoTemplate mongoTemplate(){
        return new MongoTemplate(mongoClient(),dataBaseName);
    }


    @Bean
    @Primary
    public MongoClientOptions mongoClientOptions(){
        MongoClientOptions.Builder build = MongoClientOptions.builder();
        build.maxWaitTime(maxWaitTime);
        build.connectTimeout(connectTimeout);
        build.socketTimeout(socketTimeOut);
        build.socketKeepAlive(false);
        //连接池设置为300个连接,默认为100
        build.connectionsPerHost(connections);
        //线程队列数,如果连接线程排满了队列就会抛出 Out of semaphores to get db 错误
        build.threadsAllowedToBlockForConnectionMultiplier(5000);
        build.writeConcern(WriteConcern.ACKNOWLEDGED);
        return build.build();
    }


    @Value("${mongo.iscluster}")
    private Boolean isCluster;
    @Value("${mongo.hosts}")
    private String hosts;
    @Value("${mongo.port}")
    private Integer port;
    @Value("${mongo.username}")
    private String userName;
    @Value("${mongo.password}")
    private String passWord;
    @Value("${mongo.auth}")
    private String auth;
    @Value("${mongo.authentication}")
    private Boolean authentication;
    @Value("${mongo.maxwaittime}")
    private Integer maxWaitTime;
    @Value("${mongo.connecttimeout}")
    private Integer connectTimeout;
    @Value("${mongo.connections}")
    private Integer connections;
    @Value("${mongo.socketime}")
    private Integer socketTimeOut;
    @Value("${mongo.database}")
    private String dataBaseName;
}
